package io.dora.zk;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.Stat;

public class CuratorSetTest {
	
private CuratorFramework zkClient;
	
	public CuratorSetTest() {
		zkClient = CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181").retryPolicy(new RetryNTimes(2000,20000)).build();
		zkClient.start();
	}
	
	public void setData(String node, String data) {
		try {
			Stat stat = zkClient.checkExists().forPath(node);
			if (stat == null) {
				zkClient.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE).forPath(node);
			}
			zkClient.setData().forPath(node, data.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			String p = FileUtils.readFileToString(new File("d:/jdbc-update.properties"));
			CuratorSetTest cst = new CuratorSetTest();
			cst.setData("/jshop-cx/jshop/config-db/yf-rw", p);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
